
package com.kugou.game.sdk.persistence.preference;

import com.kugou.game.sdk.utils.AppUtil;

import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;

/**
 * SharePreference操作统合类，在1.3旧版基础修改部分
 * 
 * @author ljc
 * @since 2013-12-14 上午10:00:33
 */
public class PrefUtil {

    public static final String PREFERENCE_FILENAME = "_preferences";

    public static final String PREFERENCE_SETTING = "setting";

    // 在登录界面是否在弹出过绑定提示
    public static final String SHOW_BIND_PHONE = "sms_bind_phone";

    // 上一次弹出绑定手机号的时间
    public static final String LAST_SHOW_BIND_PHONE_TIME = "last_show_bind_phone_time";

    // 验证码发送次数
    public static final String SMS_CODE_TIMES = "sms_code_times";

    // 验证码发送那天
    public static final String SETTING_GET_VERIFIY_DATE = "verifiyDate";

    /** ******************************************************************************************/

    // 设置每天凌晨时间0：00
    public static final String SHOW_EVERY_ZERO_DATE = "Show_Every_Zero_Date";

    // 验证码发送次数
    public static final String GET_PASSWORD_SMS_CODE_TIMES = "set_password_sms_code_times";

    /** ******************************************************************************************/

    // 临时账号键值对name
    public static final String TEMPORARY_ACCOUNT_CODE_TIMES = "temporary_account_code_times";

    // 设置登录的次数
    public static final String TEMPORARY_LOGIN_CODE_TIMES = "temporary_login_code_times";

    // 设置每天凌晨时间0：00
    public static final String TEMPORARY_ACCOUNT_ZERO_DATE = "temporary_account_Zero_Date";

    // 是否已经下载了省份和城市
    public static final String IS_DOWNLOAD_ADRESS = "is_download_province_and_city";

    /** 消息版本号 */
    public final static String MESSAGE_ID = "push_message_version";

    /**
     * 验证码发送次数 + 1
     * 
     * @param context
     */
    public static void addSmsCodeSendTimes(Context context) {
        SharedPreferences sp = context.getSharedPreferences(PREFERENCE_SETTING,
                Context.MODE_WORLD_READABLE | Context.MODE_WORLD_WRITEABLE);
        sp.edit().putInt(SMS_CODE_TIMES, getSmsCodeSendTimes(context) + 1).commit();
    }

    /**
     * 验证码发送次数清零
     * 
     * @param context
     */
    public static void clearSmsCodeSendTimes(Context context) {
        SharedPreferences sp = context.getSharedPreferences(PREFERENCE_SETTING,
                Context.MODE_WORLD_READABLE | Context.MODE_WORLD_WRITEABLE);
        sp.edit().putInt(SMS_CODE_TIMES, 0).commit();
    }

    /**
     * 获取验证码发送次数
     * 
     * @param context
     * @return
     */
    public static int getSmsCodeSendTimes(Context context) {
        SharedPreferences sp = context.getSharedPreferences(PREFERENCE_SETTING,
                Context.MODE_WORLD_READABLE | Context.MODE_WORLD_WRITEABLE);
        return sp.getInt(SMS_CODE_TIMES, 0);
    }

    /**
     * 获取当天验证码获取的时间
     * 
     * @param context
     * @return
     */
    public static long getSendSmsCodeDate(Context context) {
        SharedPreferences sp = context.getSharedPreferences(PREFERENCE_SETTING,
                Context.MODE_WORLD_READABLE | Context.MODE_WORLD_WRITEABLE);
        return sp.getLong(SETTING_GET_VERIFIY_DATE, System.currentTimeMillis() / 1000);
    }

    /**
     * 设置当天验证码获取的时间
     * 
     * @return
     */
    public static void setSendSmsCodeDate(Context context, long time) {
        SharedPreferences sp = context.getSharedPreferences(PREFERENCE_SETTING,
                Context.MODE_WORLD_READABLE | Context.MODE_WORLD_WRITEABLE);
        sp.edit().putLong(SETTING_GET_VERIFIY_DATE, time).commit();
    }

    /**
     * 在登录界面是否在弹出过绑定提示
     * 
     * @param context
     * @return
     */
    public static boolean isShowBindPhoneTips(Context context) {
        SharedPreferences sp = context.getSharedPreferences(PREFERENCE_SETTING,
                Context.MODE_WORLD_READABLE | Context.MODE_WORLD_WRITEABLE);
        return sp.getBoolean(SHOW_BIND_PHONE, true);
    }

    /**
     * 设置登录界面是否在弹出过绑定提示
     * 
     * @param context
     * @return
     */
    public static void setShowBindPhoneTips(Context context, boolean isShow) {
        SharedPreferences sp = context.getSharedPreferences(PREFERENCE_SETTING,
                Context.MODE_WORLD_READABLE | Context.MODE_WORLD_WRITEABLE);
        sp.edit().putBoolean(SHOW_BIND_PHONE, isShow).commit();
    }

    /**
     * 获取上一次弹出绑定手机号的时间
     * 
     * @param context
     * @return
     */
    public static long getLastShowBindPhoneDate(Context context) {
        SharedPreferences sp = context.getSharedPreferences(PREFERENCE_SETTING,
                Context.MODE_WORLD_READABLE | Context.MODE_WORLD_WRITEABLE);
        return sp.getLong(LAST_SHOW_BIND_PHONE_TIME, 0);
    }

    /**
     * 设置当天验证码获取的时间
     * 
     * @return
     */
    public static void setLastShowBindPhoneDate(Context context, long time) {
        SharedPreferences sp = context.getSharedPreferences(PREFERENCE_SETTING,
                Context.MODE_WORLD_READABLE | Context.MODE_WORLD_WRITEABLE);
        sp.edit().putLong(LAST_SHOW_BIND_PHONE_TIME, time).commit();
    }

    /** ******************************************************************************************/

    public static void setShowEveryZeroDate(Context context) {
        SharedPreferences sp = context.getSharedPreferences(PREFERENCE_SETTING,
                Context.MODE_WORLD_READABLE | Context.MODE_WORLD_WRITEABLE);
        sp.edit().putLong(SHOW_EVERY_ZERO_DATE, System.currentTimeMillis()).commit();
    }

    public static long getShowEveryZeroDate(Context context) {
        SharedPreferences sp = context.getSharedPreferences(PREFERENCE_SETTING,
                Context.MODE_WORLD_READABLE | Context.MODE_WORLD_WRITEABLE);
        return sp.getLong(SHOW_EVERY_ZERO_DATE, System.currentTimeMillis());
    }

    /**
     * 验证码发送次数 + 1
     * 
     * @param context
     */
    public static void setSmsCodeSendTimesGetPassword(Context context) {
        SharedPreferences sp = context.getSharedPreferences(PREFERENCE_SETTING,
                Context.MODE_WORLD_READABLE | Context.MODE_WORLD_WRITEABLE);
        sp.edit().putInt(GET_PASSWORD_SMS_CODE_TIMES, getSmsCodeSendTimesGetPassword(context) + 1)
                .commit();
    }

    /**
     * 验证码发送次数清零
     * 
     * @param context
     */
    public static void clearSmsCodeSendTimesGetPassword(Context context) {
        SharedPreferences sp = context.getSharedPreferences(PREFERENCE_SETTING,
                Context.MODE_WORLD_READABLE | Context.MODE_WORLD_WRITEABLE);
        sp.edit().putInt(GET_PASSWORD_SMS_CODE_TIMES, 0).commit();
    }

    /**
     * 获取验证码发送次数
     * 
     * @param context
     * @return
     */
    public static int getSmsCodeSendTimesGetPassword(Context context) {
        SharedPreferences sp = context.getSharedPreferences(PREFERENCE_SETTING,
                Context.MODE_WORLD_READABLE | Context.MODE_WORLD_WRITEABLE);
        return sp.getInt(GET_PASSWORD_SMS_CODE_TIMES, 0);
    }

    /** ******************************************************************************************/

    /**
     * 设置每天的凌晨时间0：00
     * 
     * @return
     */
    public static void setShowZeroDate(Context context, String name, String value) {
        SharedPreferences sp = context.getSharedPreferences(name, Context.MODE_WORLD_READABLE
                | Context.MODE_WORLD_WRITEABLE);
        sp.edit().putLong(value, System.currentTimeMillis() / 1000 / 60 / 60 / 24 * 60 * 60 * 24)
                .commit();
    }

    /**
     * 获取每天的凌晨时间0：00
     * 
     * @param context
     * @return
     */
    public static long getShowZeroDate(Context context, String name, String value) {
        SharedPreferences sp = context.getSharedPreferences(name, Context.MODE_WORLD_READABLE
                | Context.MODE_WORLD_WRITEABLE);
        return sp.getLong(value, System.currentTimeMillis() / 1000 / 60 / 60 / 24 * 60 * 60 * 24);
    }

    /**
     * 验证码发送次数 + 1
     * 
     * @param context
     */
    public static void setSmsCodeSendTimesGetPassword(Context context, String name, String value) {
        SharedPreferences sp = context.getSharedPreferences(name, Context.MODE_WORLD_READABLE
                | Context.MODE_WORLD_WRITEABLE);
        sp.edit()
                .putInt(value,
                        getSmsCodeSendTimes(context, TEMPORARY_ACCOUNT_CODE_TIMES,
                                TEMPORARY_LOGIN_CODE_TIMES) + 1).commit();
    }

    /**
     * 验证码发送次数清零
     * 
     * @param context
     */
    public static void clearSmsCodeSendTimes(Context context, String name, String value) {
        SharedPreferences sp = context.getSharedPreferences(name, Context.MODE_WORLD_READABLE
                | Context.MODE_WORLD_WRITEABLE);
        sp.edit().putInt(value, 0).commit();
    }

    /**
     * 获取验证码发送次数
     * 
     * @param context
     * @return
     */
    public static int getSmsCodeSendTimes(Context context, String name, String value) {
        SharedPreferences sp = context.getSharedPreferences(name, Context.MODE_WORLD_READABLE
                | Context.MODE_WORLD_WRITEABLE);
        return sp.getInt(value, 0);
    }

    /**
     * 验证码用途类型--用于找回密码
     */
    public static final int TYPE_RETRIEVER_PSD = 1;

    /**
     * 验证码用途类型--用于绑定手机
     */
    public static final int TYPE_BIND_PHONE = 2;

    /**
     * 获取不同用途 已发送验证码次数
     * 
     * @param context
     * @param type 验证码用途类型
     * @return
     */
    public static int getRemainValidateTimes(Context context, int type) {
        SharedPreferences sp = context.getSharedPreferences(PREFERENCE_SETTING,
                Context.MODE_WORLD_READABLE | Context.MODE_WORLD_WRITEABLE);
        return sp.getInt(AppUtil.getTodayDate() + "-" + type, 0);
    }

    /**
     * 保存验证码已发送次数
     * 
     * @param context
     * @param type 验证码用途类型
     * @param times 已发送次数
     */
    public static void putRemainValidateTimes(Context context, int type, int times) {
        SharedPreferences sp = context.getSharedPreferences(PREFERENCE_SETTING,
                Context.MODE_WORLD_READABLE | Context.MODE_WORLD_WRITEABLE);
        Editor editor = sp.edit();
        editor.putInt(AppUtil.getTodayDate() + "-" + type, times);
        editor.commit();
    }

    public static boolean getIsDownloadAdress(Context context) {
        SharedPreferences sp = context.getSharedPreferences(PREFERENCE_SETTING,
                Context.MODE_WORLD_READABLE | Context.MODE_WORLD_WRITEABLE);
        return sp.getBoolean(IS_DOWNLOAD_ADRESS, false);
    }

    public static void setIsDownloadAdress(Context context, boolean flag) {
        SharedPreferences sp = context.getSharedPreferences(PREFERENCE_SETTING,
                Context.MODE_WORLD_READABLE | Context.MODE_WORLD_WRITEABLE);
        sp.edit().putBoolean(IS_DOWNLOAD_ADRESS, flag).commit();
    }

    public static void setMessageVersion(Context context, String messageVersionId) {
        SharedPreferences sp = context.getSharedPreferences(PREFERENCE_SETTING,
                Context.MODE_WORLD_READABLE | Context.MODE_WORLD_WRITEABLE);
        sp.edit().putString(MESSAGE_ID, messageVersionId).commit();
    }

    public static String getMessageVersion(Context context) {
        SharedPreferences sp = context.getSharedPreferences(PREFERENCE_SETTING,
                Context.MODE_WORLD_READABLE | Context.MODE_WORLD_WRITEABLE);
        return sp.getString(MESSAGE_ID, "0");
    }

    private static final String GAME_CENTER_RED_POINT = "game_center_red_point";

    private static final String GAME_CENTER_RED_POINT_TEMP = "game_center_red_point_temp";

    public static void setGamecenterRedpointData(Context context, String data) {
        SharedPreferences sp = context.getSharedPreferences(PREFERENCE_SETTING,
                Context.MODE_WORLD_READABLE | Context.MODE_WORLD_WRITEABLE);
        sp.edit().putString(GAME_CENTER_RED_POINT, data).commit();
    }

    public static String getGamecenterRedpointData(Context context) {
        SharedPreferences sp = context.getSharedPreferences(PREFERENCE_SETTING,
                Context.MODE_WORLD_READABLE | Context.MODE_WORLD_WRITEABLE);
        return sp.getString(GAME_CENTER_RED_POINT, "");
    }

    public static void setGamecenterRedpointDataTemp(Context context, String data) {
        SharedPreferences sp = context.getSharedPreferences(PREFERENCE_SETTING,
                Context.MODE_WORLD_READABLE | Context.MODE_WORLD_WRITEABLE);
        sp.edit().putString(GAME_CENTER_RED_POINT_TEMP, data).commit();
    }

    public static String getGamecenterRedpointDataTemp(Context context) {
        SharedPreferences sp = context.getSharedPreferences(PREFERENCE_SETTING,
                Context.MODE_WORLD_READABLE | Context.MODE_WORLD_WRITEABLE);
        return sp.getString(GAME_CENTER_RED_POINT_TEMP, "");
    }

    private static final String KUGOU_PLATFORM_UUID = "kugou_platform_uuid";

    public static void setKugouUUID(Context context, String uuid) {
        SharedPreferences sp = context.getSharedPreferences(PREFERENCE_SETTING,
                Context.MODE_WORLD_READABLE | Context.MODE_WORLD_WRITEABLE);
        sp.edit().putString(KUGOU_PLATFORM_UUID, uuid).commit();
    }

    public static String getKugouUUID(Context context) {
        SharedPreferences sp = context.getSharedPreferences(PREFERENCE_SETTING,
                Context.MODE_WORLD_READABLE | Context.MODE_WORLD_WRITEABLE);
        return sp.getString(KUGOU_PLATFORM_UUID, "");
    }

    private static final String FIRST_START = "is_kugou_game_app_first_start";

    public static void setAppFirstStart(Context context, boolean firstStart) {
        SharedPreferences sp = context.getSharedPreferences(PREFERENCE_SETTING,
                Context.MODE_WORLD_READABLE | Context.MODE_WORLD_WRITEABLE);
        sp.edit().putBoolean(FIRST_START, firstStart).commit();
    }

    public static boolean getAppFirstStart(Context context) {
        SharedPreferences sp = context.getSharedPreferences(PREFERENCE_SETTING,
                Context.MODE_WORLD_READABLE | Context.MODE_WORLD_WRITEABLE);
        return sp.getBoolean(FIRST_START, true);
    }

    private static final String SHOWED_SET_PAY_PSD_TIP = "showed_set_pay_psd_tip";

    /**
     * 设置支付密码之后是否提示安全信息
     * 
     * @param context
     * @param isNeedShow
     */
    public static void setPaySecurityTipNeedShow(Context context, String username,
            boolean isNeedShow) {
        SharedPreferences sp = context.getSharedPreferences(PREFERENCE_SETTING,
                Context.MODE_WORLD_READABLE | Context.MODE_WORLD_WRITEABLE);
        sp.edit().putBoolean(SHOWED_SET_PAY_PSD_TIP + username, isNeedShow).commit();
    }

    /**
     * 获取设置支付密码之后是否提示安全信息
     * 
     * @param context
     * @return
     */
    public static boolean getPaySecurityTipNeedShow(Context context, String username) {
        SharedPreferences sp = context.getSharedPreferences(PREFERENCE_SETTING,
                Context.MODE_WORLD_READABLE | Context.MODE_WORLD_WRITEABLE);
        return sp.getBoolean(SHOWED_SET_PAY_PSD_TIP + username, true);
    }

    public static int getUsePatchId(Context context, String key) {
        SharedPreferences sp = context.getSharedPreferences(PREFERENCE_SETTING,
                Context.MODE_WORLD_READABLE | Context.MODE_WORLD_WRITEABLE);
        return sp.getInt(key, 0);
    }

    public static void putUsePatchId(Context context, String key, int patchId) {
        SharedPreferences sp = context.getSharedPreferences(PREFERENCE_SETTING,
                Context.MODE_WORLD_READABLE | Context.MODE_WORLD_WRITEABLE);
        sp.edit().putInt(key, patchId).commit();
    }
}
